<?php
namespace Admin\Controller;

class Class2Controller extends BaseController {

    public function index() {

        $ClassManager = D ('ClassManager');
        $cids = $ClassManager->getClassIdsByTid($this->admin['id']);

        $Class = D ('Class');
        $classList = $Class->getClassArrByIdArr($cids);

        // 获取班级人数以及班长和团支书信息
        $Student = D ('Student');
        foreach ($classList as $k=>$v) {
            $classList[$k]['count'] = $Student->getCountByCid($v['id']);
            $classList[$k]['monitor'] = $Student->_get($v['monitor']);
            $classList[$k]['tzs'] = $Student->_get($v['tzs']);
        }

        $this->assign('classList',$classList);
        $this->display();
    }


    public function student() {

        $class_id = I ('request.class_id');
        if (empty($class_id)) $this->error('非法访问');

        $Class = D ('Class');
        $class_name = $Class->getFullClassName($class_id);
        if (empty($class_name)) $this->error('班级不存在');

        $ClassManager = D ('ClassManager');
        $teacher_id = $ClassManager->getTidByCid($class_id);
        if ($teacher_id !== $this->admin['id']) $this->error('你没有权限管理该班级');

        $Student = D ('Student');
        $map['class_id'] = $class_id;

        $limit = \Api\Libs\PageUtil::getLimit(10);
        $page = \Api\Libs\PageUtil::getPageHtml($Student->getCountByWhere($map),10);

        $studentList = $Student->getStudentByWhere($map,$limit);

        $this->assign('studentList',$studentList);
        $this->assign('page',$page);
        $this->assign('class_name',$class_name);
        $this->assign('class_id',$class_id);
        $this->display();
    }

    // 添加学生
    public function sadd() {

        $class_id = I ('request.class_id');
        if (empty($class_id)) $this->error('非法访问');

        $Class = D ('Class');
        $c = $Class->_get($class_id);
        if (empty($c)) $this->error('班级不存在');
        $c['class_name'] = $Class->getFullClassName($class_id);

        $ClassManager = D ('ClassManager');
        $teacher_id = $ClassManager->getTidByCid($class_id);
        if ($teacher_id !== $this->admin['id']) $this->error('你没有权限管理该班级');

        if (!IS_POST) {
            $this->assign('class',$c);
            $this->display();
        } else {

            $data['class_id'] = I ('request.class_id');
            $data['student_id'] = I ('request.student_id');
            $data['name'] = I ('request.name');
            $data['password'] = md5('123456');
            $data['sex'] = I ('request.sex');
            $data['telphone'] = I ('request.telphone');
            $data['profession_id'] = D ('Class')->getProfessionId($data['class_id']);
            $data['department_id'] = $this->admin['department_id'];

            if (($flag = \Api\Libs\HttpVerifyUtil::verifyFormParamNotNull($data)) !== '000000')
                $this->error(\Api\Libs\ExplainRespCodeUtil::explain($flag));

            $ClassManager = D ('ClassManager');
            $teacher_id = $ClassManager->getTidByCid($data['class_id']);
            if ($teacher_id !== $this->admin['id']) $this->error('你没有权限向该班级添加学生');

            $Student = D ('Student');
            $flag = $Student->_add($data);
            if ($flag === '000000')
                $this->out('RESULT_OK','',__CONTROLLER__.'/student/class_id/'.$data['class_id']);
            $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));

        }
    }


    // 修改学生
    public function sedit() {

        if (!IS_POST) {

            $id = I ('request.id');
            if (($flag = \Api\Libs\HttpVerifyUtil::verifyFormParamNotNull($id)) !== '000000')
                $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));

            $Student = D ('Student');
            $s = $Student->_find($id);
            if (empty($s))
                $this->out('RESULT_FAIL','学生不存在');

            $ClassManager = D ('ClassManager');
            $teacher_id = $ClassManager->getTidByCid($s['class_id']);
            if ($teacher_id !== $this->admin['id']) $this->error('你没有权限管理该班级里面的学生');

            $s['class_name'] = D ('Class')->getFullClassName($s['class_id']);

            $this->assign('student',$s);
            $this->display();
        } else {

            $data['id'] = I ('request.id');
            $data['student_id'] = I ('request.student_id');
            $data['name'] = I ('request.name');
            $data['sex'] = I ('request.sex');
            $data['telphone'] = I ('request.telphone');
            $data['password'] = md5(I ('request.password',''));

            if ($data['password'] === md5('default')) unset($data['password']);
            if (($flag = \Api\Libs\HttpVerifyUtil::verifyFormParamNotNull($data)) !== '000000')
                $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));

            $Student = D ('Student');
            $s = $Student->find($data['id']);
            if (empty($s))
                $this->out('RESULT_FAIL','学生不存在');

            $ClassManager = D ('ClassManager');
            $teacher_id = $ClassManager->getTidByCid($s['class_id']);
            if ($teacher_id !== $this->admin['id']) $this->error('你没有权限管理该班级里面的学生');

            $flag = $Student->_save($data);
            if ($flag === '000000')
                $this->out('RESULT_OK','',__CONTROLLER__.'/student/class_id/'.$s['class_id']);
            $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));
        }

    }

    /**
     * 审核学生头像信息
     */
    public function sphoto() {

        if (!IS_POST) {

            $id = I ('request.id',null);
            if (($flag = \Api\Libs\HttpVerifyUtil::verifyFormParamNotNull($id)) !== '000000')
                $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));

            $student = M ('Student')->find($id);
            if (empty($student))
                $this->out('RESULT_FAIL','学生不存在');

            $student['photo_status_detail'] = $this->handlerPhotoStatus($student['photo_status']);
            $this->assign('student',$student);
            $this->display();

        } else {
            $data['id'] = I ('request.id',null);
            $data['photo_status'] = I ('request.status',null);
            if (($flag = \Api\Libs\HttpVerifyUtil::verifyFormParamNotNull($data) !== '000000'))
                $this->out('RESULT_FAIL','缺少参数');
            $Model = M ('Student');
            $flag = $Model->save($data);
            if ($flag === false)
                $this->out('RESULT_FAIL','审核失败');
            $this->out('RESULT_OK','审核成功');
        }

    }

    protected function handlerPhotoStatus($status) {
        switch ($status) {
            case 0:
                return '头像未上传';
            case 1:
                return '头像未审核';
            case 2:
                return '头像审核不通过';
            case 3:
                return '头像审核通过';
        }
    }


    public function sdel() {
        if (!IS_POST) $this->error('非法访问');

        $id = I ('request.id','');
        if (($flag = \Api\Libs\HttpVerifyUtil::verifyFormParamNotNull($id)) !== '000000')
            $this->out(\Api\Libs\ExplainRespCodeUtil::explain($flag));

        $Student = D ('Student');
        $s = $Student->_find($id);
        if (empty($s)) $this->error('学生不存在');

        $ClassManager = D ('ClassManager');
        $teacher_id = $ClassManager->getTidByCid($s['class_id']);
        if ($teacher_id !== $this->admin['id']) $this->error('你没有权限管理该班级里面的学生');

        $flag = $Student->_del($id);
        if ($flag === '000000')
            $this->out('RESULT_OK','',__CONTROLLER__.'/student/class_id/'.$s['class_id']);
        $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));
    }


    /**
     * 导入学生数据
     */
    public function import(){

        if (!IS_POST) {

            $class_id = I ('request.class_id');
            if (empty($class_id)) $this->error('非法访问');
            $Class = D ('Class');
            $c = $Class->_get($class_id);
            if (empty($c)) $this->error('班级不存在');
            $c['class_name'] = $Class->getFullClassName($class_id);

            $ClassManager = D ('ClassManager');
            $teacher_id = $ClassManager->getTidByCid($class_id);
            if ($teacher_id !== $this->admin['id']) $this->error('你没有权限管理该班级');

            $this->assign('class',$c);
            $this->display();
        } else {

            $class_id = I ('request.class_id');
            if (empty($class_id)) $this->error('非法访问');

            $Student = D("Student");
            if(isset($_FILES["file"]) && ($_FILES["file"]["error"] == 0)){
                $result = importExecl($_FILES["file"]["tmp_name"]);
                //p($result);
                if($result["error"] == 1){
                    // 获取数据内容
                    $execl_data = $result["data"][0]["Content"];

                    $count = $execl_data["data"][0]["Rows"];
                    $successCount = $failCount = $repeatCount = 0;

                    foreach($execl_data as $k=>$v){
                        if($k != 1){  // 第一行为标题

                            $data['student_id'] = $v[0];
                            $data['name'] = $v[1];
                            $data['sex'] = $v[2];
                            $data['telphone'] = $v[3];
                            $data['class_id'] = intval($class_id);
                            $data['password'] = md5('123456');
                            $data['profession_id'] = intval(D ('Class')->getProfessionId($data['class_id']));
                            $data['department_id'] = intval($this->admin['department_id']);

                            $flag = $Student->_add($data);

                            switch ($flag) {
                                case '101301':
                                    $repeatCount ++;
                                    break;
                                case '000000':
                                    $successCount ++;
                                    break;
                                case '111111':
                                    $failCount ++;
                                    break;
                            }
                        }
                    }

                    $this->success('导入学生结束，其中成功导入 '.$successCount.' 人'.'，失败 '.$failCount.' 人，'.'冲突 '.$repeatCount.' 人', __CONTROLLER__,10);
                } else {
                    $this->error('Excel表格数据错误');
                }
            } else {
                $this->error('请先选择一个Excel文件');
            }
        }
    }

    // 班级管理
    public function edit() {

        if (!IS_POST) {
            $class_id = I ('request.id','');
            if (empty($class_id)) $this->error('非法访问');

            $Class = D ('Class');
            $c = $Class->_get($class_id);
            if (empty($c)) $this->error('班级不存在');

            $ClassManager = D ('ClassManager');
            $teacher_id = $ClassManager->getTidByCid($class_id);
            if ($teacher_id !== $this->admin['id']) $this->error('你没有权限管理该班级');

            // 获取该班级所有学生
            $Student = D ('Student');
            $studentList = $Student->getStudentByCid($class_id);

            $this->assign('class',$c);
            $this->assign('studentList',$studentList);
            $this->display();
        } else {
            $data['id'] = I ('request.class_id');
            $data['monitor'] = I ('request.monitor');
            $data['tzs'] = I ('request.tzs');
            if (($flag = \Api\Libs\HttpVerifyUtil::verifyFormParamNotNull($data)) !== '000000')
                $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));

            $Class = D ('Class');
            $flag = $Class->editClassInfo($data);
            if ($flag === '000000')
                $this->out('RESULT_OK','',__CONTROLLER__);
            $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));
        }
    }

    public function in() {
        p($_FILES);
    }


    protected function studentActionVerify() {

    }

    protected function checkCid($class_id) {


    }

}